package com.vgooo.client.tool.tester;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.vgooo.client.R;
import com.vgooo.client.VGActivity;
import com.vgooo.client.myvg.tencent.QQOauthorizeAction;

public class TestActivity extends VGActivity {

	public ListView listView;

	public ArrayList<Map<String, Object>> list = null;
	public TestAdapter testAdapter;

	// 测试项目
	public int[] titleItem = { R.string.test_display_title,
			R.string.test_light_title, R.string.test_shake_title,
			R.string.test_touch_title, R.string.test_audio_title,
			R.string.test_camera_title, R.string.test_commun_title };
	public int[] contentItem = { R.string.test_display_content,
			R.string.test_light_content, R.string.test_shake_content,
			R.string.test_touch_content, R.string.test_audio_content,
			R.string.test_camera_content, R.string.test_commun_content };
	public int[] valueItem = { R.string.test_display_value,
			R.string.test_light_value, R.string.test_shake_value,
			R.string.test_touch_value, R.string.test_audio_value,
			R.string.test_camera_value, R.string.test_commun_value };

	public Button runButton;
	public Button selectallButton;
	public Button unselectallButton;
	public boolean isSelectAll = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		setContentView(R.layout.test_main);

		listView = (ListView) findViewById(R.id.test_list);
		if (getTests() != null && list != null) {
			testAdapter = new TestAdapter(this);

			listView.setAdapter(testAdapter);
		}

		runButton = (Button) findViewById(R.id.run);
		selectallButton = (Button) findViewById(R.id.selectall);
		unselectallButton = (Button) findViewById(R.id.unselectall);

		runButton.setOnClickListener(new View.OnClickListener() {

			@SuppressWarnings({ "unused", "null" })
			@Override
			public void onClick(View v) {

				if (titleItem.length > 0) {

					ArrayList<String> values = new ArrayList<String>();
					for (int i = 0; i < list.size(); i++) {
						HashMap<String, Object> map = (HashMap<String, Object>) list
								.get(i);
						if ((String) map.get("checked") == "1") {
							values.add((String) map.get("value"));
						}

					}
					if (values.size() > 0) {
						Intent intent = new Intent(TestActivity.this,
								TestRunActivity.class);

						Bundle bundle = new Bundle();
						bundle.putStringArrayList("values", values);

						intent.putExtras(bundle);

						startActivity(intent);
					} else {
						Toast.makeText(TestActivity.this, "选择测试项目",
								Toast.LENGTH_SHORT).show();
					}
				}
			}
		});
		selectallButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				isSelectAll = true;
				testAdapter.notifyDataSetChanged();

			}
		});
		unselectallButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				isSelectAll = false;
				testAdapter.notifyDataSetChanged();

			}
		});
	}
	
	boolean flag = true ;
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		// 友盟统计功能，打开页面
		MobclickAgent.onResume(this);
		super.onResume();
		if(flag){
			flag = false ;
			new QQOauthorizeAction(this, null, null);
		}
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		// 友盟统计功能，页面关闭
		MobclickAgent.onPause(this);
		super.onPause();
	}

	// 获取测试项目
	public ArrayList<Map<String, Object>> getTests() {

		if (titleItem.length > 0) {

			list = new ArrayList<Map<String, Object>>();

			for (int i = 0; i < titleItem.length; i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("title", getResources().getString(titleItem[i]));
				map.put("content", getResources().getString(contentItem[i]));
				map.put("value", getResources().getString(valueItem[i]));
				map.put("checked", "0");

				list.add(map);
			}

		}

		return list;
	}

	// 自定义列表适配器
	private class TestAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		private Context mContext = null;

		public TestAdapter(Context context) {
			mContext = context;
			mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		public CheckBox titleView;
		public TextView contentView;

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.test_item, null);
			}

			titleView = (CheckBox) convertView.findViewById(R.id.title);
			contentView = (TextView) convertView.findViewById(R.id.content);

			HashMap<String, Object> map = (HashMap<String, Object>) list
					.get(position);
			titleView.setText((String) map.get("title"));
			titleView.setTag((String) map.get("value"));
			if (isSelectAll) {
				titleView.setChecked(true);
				map.put("checked", "1");
			} else {
				titleView.setChecked(false);
				map.put("checked", "0");
			}
			contentView.setText((String) map.get("content"));
			titleView.setOnCheckedChangeListener(new CheckBoxChecked(position));

			return convertView;
		}

		public class CheckBoxChecked implements
				CompoundButton.OnCheckedChangeListener {

			private int position;

			public CheckBoxChecked(int mPosition) {
				position = mPosition;
			}

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {

				HashMap<String, Object> map = (HashMap<String, Object>) list
						.get(position);

				if (isChecked) {
					map.put("checked", "1");
				} else {
					map.put("checked", "0");
				}
			}

		}

	}

//	@Override
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		if (keyCode == KeyEvent.KEYCODE_BACK) {
//
//			Intent intent2 = new Intent(this, IndexActivity.class);
//
//			startActivity(intent2);
//			finish();
//
//			return false;
//		}
//		return super.onKeyDown(keyCode, event);
//	}
}
